package com.gitee.cashzhang27.test.cloud.oauth.auth.server.jdbc.configuration;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * spring security配置
 *
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/01/26 11:52
 */
@Slf4j
@Primary
@Configuration

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  @SneakyThrows
  protected void configure(HttpSecurity http) {
    http
        .formLogin()
        .loginPage("/token/login")
        .loginProcessingUrl("/token/form")
        .and()
        .authorizeRequests()
        .antMatchers("/actuator/**").permitAll()
        .anyRequest().authenticated()
        .and().csrf().disable();
  }

  /**
   * 不拦截静态资源
   */
  @Override
  public void configure(WebSecurity web) {
    web.ignoring().antMatchers("/css/**");
  }

  @Bean
  @Override
  @SneakyThrows
  public AuthenticationManager authenticationManagerBean() {
    return super.authenticationManagerBean();
  }


  /**
   * https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated
   * Encoded password does not look like BCrypt
   *
   * @return PasswordEncoder
   */
  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }
}
